package com.example.administrator.myapplication;


import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.RelativeLayout;

import com.example.administrator.myapplication.login.AccountActivity;
import com.example.administrator.myapplication.service.LoginJudge;

import java.util.Timer;
import java.util.TimerTask;


/**
 * A simple {@link Fragment} subclass.
 * Use the {@link ExitFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class ExitFragment extends DialogFragment {
    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    private static final String TAG = "ExitFragment";
    private ActionBar actionBar;
    private RelativeLayout log_out_relative;
    private RelativeLayout system_exit_relative;
    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;


    public ExitFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment ExitFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static ExitFragment newInstance(String param1, String param2) {
        ExitFragment fragment = new ExitFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }

    }



    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
       getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        View view = inflater.inflate(R.layout.fragment_exit, container, false);
        log_out_relative = ((RelativeLayout) view.findViewById(R.id.log_out_relative));
        system_exit_relative = ((RelativeLayout) view.findViewById(R.id.system_exit_relative));

        log_out_relative.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MyBaseApplication.showToastMake("正在注销账户！").show();
                LoginJudge loginJudge = new LoginJudge(getActivity());
                if(loginJudge.write_off_accounts(null)){
                    Timer timer = new Timer();
                    timer.schedule(new TimerTask() {
                        @Override
                        public void run() {
                            Intent intent = new Intent(getActivity(), AccountActivity.class);
                            startActivity(intent);
                        }
                    },500);

                }

            }
        });
        system_exit_relative.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Handler handler = new Handler();
                MyBaseApplication.showToastMake("等待中。。。。。。").show();
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        System.exit(0);
                    }
                },1000);

            }
        });

        // Inflate the layout for this fragment
        return view;
    }

}
